package com.sensePlusVR.bean;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

import android.content.Context;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.base.EntityBase;
import com.sensePlusVR.utils.MyLog;

@Table(name = "GameUrl")
public class GameUrl extends EntityBase{
	
	private static final String TAG = GameUrl.class.getSimpleName();
	
	@Column(name = "url")
	private String url;//下载url
	
	@Column(name = "fileName")
	private String fileName;
	
	@Column(name = "downloadCount")
	private int downloadCount;//下载次数
	
	@Column(name = "savePath")
	private String savePath;//下载路径
	
	@Column(name = "md5")
	private String md5;
	
	@Column(name = "downloadState")
	private int downloadState;//下载状态
	
	@Column(name = "totalLength")
	private long totalLength;//文件总长度
	
	
	
	
	

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getDownloadCount() {
		return downloadCount;
	}

	public void setDownloadCount(int downloadCount) {
		this.downloadCount = downloadCount;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

	public int getDownloadState() {
		return downloadState;
	}

	public void setDownloadState(int downloadState) {
		this.downloadState = downloadState;
	}

	public long getTotalLength() {
		return totalLength;
	}

	public void setTotalLength(long totalLength) {
		this.totalLength = totalLength;
	}
	
	

	@Override
	public String toString() {
		return "GameUrl [url=" + url + ", fileName=" + fileName + ", downloadCount=" + downloadCount + ", savePath="
				+ savePath + ", md5=" + md5 + ", downloadState=" + downloadState + ", totalLength=" + totalLength + "]";
	}

	/**
	 * 
	 * @param gameUrl
	 */
	public static void saveOrUpdate(final GameUrl gameUrl) {
		try {
			Context context = VRApplication.getAppContext();
			if (context != null) {
				DbManager db = VRApplication.getDbManager();
				db.saveOrUpdate(gameUrl);

			}
		} catch (Exception e) {
			MyLog.e(TAG, "GameUrl saveOrupdate failed: " + e);
		}
	}
	
	/**
	 * 根据url查询
	 * @param url
	 * @return
	 */
	public static GameUrl query(String url){
		
		try {
    		Context context = VRApplication.getAppContext();
    		if(context != null) {
    			DbManager db = VRApplication.getDbManager();
	    		return db.selector(GameUrl.class).where("url","=",url).findFirst();
    		}
    	}catch(Exception e){
    		MyLog.e(TAG, "query:"+url+" failed: "+e);
    	}
    	return null;
	}
	

	
	
	/**
	 * 删除
	 * @param gameUrl
	 */
	public static void delete(final GameUrl gameUrl) {
		new Thread() {
			public void run() {
				try {
					Context context = VRApplication.getAppContext();
					if (context != null) {
						DbManager db = VRApplication.getDbManager();
						db.delete(gameUrl);

					}
				} catch (Exception e) {
					MyLog.e(TAG, "delete delete failed: " + e);
				}
			};
		}.start();
	}

}
